package com.example.wac.enums;

import com.example.wac.exception.AppException;

import java.text.MessageFormat;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
 * @author wuancheng
 * @description 通用错误菜单
 * @date 2021-12-30 17:33:00
 */

@SuppressWarnings("all")
public enum ErrorCode {


    OFTEN_EXCEPTION("WAC-1000","通用异常[{0}]"),
    SYSTEM_ERROR("WAC-1001", "系统错误"),
    TOKEN_ERROR("WAC-1002","token 无效，请重新获取[{0}]"),
    TOKEN_INVALID("WAC-1003","token 不存在，请重新获取[{0}]"),
    PARAM_CANNOT_EMPTY("WAC-1004","参数[{}]不能为空"),
    TOO_MANY_CURRENT_REQUESTS("WAC-1005","当前请求人数过多请稍后再试"),
    STOCK_NOT_ENOUGH("WAC-1006","当前商品库存不足"),
    EXPORT_EXCEL_ERROR("WAC-1007", "Excel文件导出失败"),
    EMAIL_ERROR("WAC-1008","邮件发送失败[{0}]"),
    FILE_TYPE_ERORR("WAC-1009", "文件格式错误"),
    IMAGE_TYPE_ERROR("WAC-1010","图片格式错误"),
    IMAGE_UPLOAD_ERROR("WAC-1011","文件上传失败"),
    GOODS_INIT_ERROR("WAC-1012", "商品初始化异常[{0}]"),
    EXPORT_NUM_MORE_THAN_MAX("WAC-1013", "上传数量超过{0}"),
    FILE_DOES_NOT_EXIST("WAC-1014","该文件不存在"),
    EXCEED_SMS_COUNT("WAC-1015","超过短信发送最大次数"),
    PHONE_NUMBER_IS_ILLEGAL("WAC-1016","手机号不合法"),
    HAVE_NON_UNIQUE_NAME("WAC-1017","存在不唯一的用户名"),
    CAPTCHA_ERROR("WAC-1018", "验证码错误"),
    LOGIN_INFO_NOT_EXISTS("WAC-1019", "用户不存在"),
    INCORRECT_CREDENTIALS_ERROR("WAC-1020", "用户名或密码错误"),
    SESSION_TIMEOUT_ERROR("WAC-1021", "会话过期失效，请重新登录"),
    EXCEED_TIME_FORBIDDEN_ERROR("WAC-1022", "密码输入错误累计已达{0}次，账号已被锁定"),
    RESTRICTED_TIME_FORBIDDEN_LOGIN_ERROR("WAC-1023", "密码输入错误累计已大于3次，1小时内禁止登录，否则账号将锁定"),
    FREQUENT_OPERATIONS("WAC-1024", "操作频繁请稍后再试"),
    TOKEN_EMPTY_ERROR("WAC-1025", "token不能为空"),
    TOKEN_PARSE_ERROR("WAC--1026", "token解析异常"),
    BLOCKED_USER("WAC-1027", "前方拥挤，请稍后重试"),
    ;

    private static Map<String, ErrorCode> errorCodeMap = new ConcurrentHashMap<>();

    static {
        ErrorCode[] values = ErrorCode.values();
        for (ErrorCode value : values) {
            ErrorCode errorCode = errorCodeMap.get(value.getCode());
            if (errorCode != null) {
                System.err.println("错误码重复:" + value.getCode());
            } else {
                errorCodeMap.put(value.getCode(), value);
            }
        }
    }

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误描述
     */
    private String desc;

    ErrorCode(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

//    public String getDescByCode(Integer code){
//        ErrorCode[] values = ErrorCode.values();
//        for (ErrorCode value : values) {
//            if (Objects.equals(value.getCode(),code)){
//                return value.getDesc();
//            }
//        }
//        return null;
//    }

    public String getDescByCode(Integer code){
        return Stream.of(ErrorCode.values())
                .filter(errorCode -> Objects.equals(errorCode.code, code))
                .findFirst()
                .map(errorCode -> errorCode.getDesc())
                .orElse(null);
    }

    public AppException getAppException(){
        return new AppException(getCode(), getDesc());
    }

    public AppException getAppException(Object param){
        return new AppException(getCode(), MessageFormat.format(getDesc(),param));
    }
}
